package com.example.service.impl;

import com.example.entity.Customer;
import com.example.entity.PageBean;
import com.example.mapper.CustomerMapper;
import com.example.service.CustomerService;
import com.example.utils.JwtUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private JwtUtils jwtUtils;
    @Override
    public PageBean page(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page,pageSize);
        List<Customer> pageList=customerMapper.selectByPage(name);
        log.info("分页查询...");
        Page<Customer> p=(Page<Customer>) pageList;
        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    @Override
    public void delete(List<Integer> ids) {
        customerMapper.delete(ids);
    }

    @Override
    public Customer findById(Integer id) {
     return customerMapper.findById(id);
    }

//    @Override
//    public void updata(Customer customer) {
//customerMapper.updata(customer);
//    }

    @Override
    public void add(Customer customer) {
        customerMapper.add(customer);
    }

    @Override
    public void updata(Customer customer) {
        if(customer.getPassword()!=null && customer.getPassword()!=""){
            String password= DigestUtils.md5DigestAsHex(customer.getPassword().getBytes());//md5加密
            customer.setPassword(password);
        }
        customerMapper.updata(customer);
    }

    @Override
    public Customer addVisitor() {
        Customer guest = new Customer();
        guest.setCustomerName("游客" + RandomStringUtils.randomAlphanumeric(8));
        guest.setUsername(RandomStringUtils.randomAlphanumeric(8) + RandomStringUtils.randomNumeric(6));
        guest.setPassword("123456");
        guest.setCustomerPhone("0000-0000-0000");
        guest.setCustomerSex("M");
        customerMapper.add(guest);
        return guest;

    }

    @Override
    public Customer login(Customer customer) {
        String password=DigestUtils.md5DigestAsHex(customer.getPassword().getBytes());//md5加密

        customer.setPassword(password);
        log.info("md5密码加密：{}",password);
        return customerMapper.findByUsernamePassword(customer);
    }
    @Override
    public Map<String,Object> getuUserinfo(String token)
    {
        Map<String, Object> claims = null;
        try {
            claims = jwtUtils.parseJWT(token);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Integer customerid = (Integer) claims.get("id");
        Customer customer=customerMapper.findById(customerid);
        if(customer != null){
            Map<String, Object> data = new HashMap<>();
            data.put("username",customer.getUsername());
            data.put("customername",customer.getCustomerName());
            data.put("customerId",customer.getCustomerId());
            data.put("telphone",customer.getCustomerPhone());
            data.put("gender",customer.getCustomerSex());
            data.put("roles","customer");
            return data;
        }
        return null;
    }
}
